package com.cs.bd.ad.self.request;

import android.content.Context;
import com.cs.bd.ad.http.bean.BaseModuleDataItemBean;
import com.cs.bd.ad.manager.AdSdkManager;
import com.cs.bd.ad.params.AdSdkParamsBuilder;
import com.cs.bd.ad.self.VirtualIdParseHelper;
import com.cs.bd.mopub.MopobViewGroupWarp;
import com.cs.bd.utils.LogUtils;

public class NativeAdFactory {

    public static BaseAdsRequest createAdsRequest(AdSdkParamsBuilder adSdkParams, String channel) {
        Context mContext = adSdkParams.mContext;int mVirtualModuleId = adSdkParams.mVirtualModuleId;
        AdSdkManager.ILoadAdvertDataListener mLoadAdvertDataListener = adSdkParams.mLoadAdvertDataListener;
        WapsAdsRequest wapsAdsRequest = new WapsAdsRequest(mContext, mLoadAdvertDataListener);
        XmAdsRequest xmAdsRequest = new XmAdsRequest(mContext, adSdkParams, VirtualIdParseHelper.parseAdId(mContext, mVirtualModuleId));
        if (isWaps(channel)) {
            if (adSdkParams.mCdays > 2) {
                return new AdsRequestPoxy(mContext, mLoadAdvertDataListener, wapsAdsRequest, xmAdsRequest);
            } else {
                return new AdsRequestPoxy(mContext, mLoadAdvertDataListener, wapsAdsRequest);
            }
        } else if (isXm(channel)) {
            if (adSdkParams.mCdays > 2) {
                return new AdsRequestPoxy(mContext, mLoadAdvertDataListener, xmAdsRequest, wapsAdsRequest);
            } else {
                return new AdsRequestPoxy(mContext, mLoadAdvertDataListener, xmAdsRequest);
            }
        } else {
            return new AdsRequestPoxy(mContext, mLoadAdvertDataListener, xmAdsRequest, wapsAdsRequest);
        }
    }

    public static boolean isWaps(String channel) {
        return channel.equals("waps");
    }

    public static boolean isXm(String channel) {
        return channel.equals("xiaomi");
    }

    public static boolean canRequestAds(AdSdkParamsBuilder adSdkParams, String channel) {
        if (null != adSdkParams.mAdControlInterceptor) {
            if (adSdkParams.mAdControlInterceptor.isLoadAd(new BaseModuleDataItemBean())) {
                if (null != adSdkParams.mLoadAdvertDataListener) {
                    adSdkParams.mLoadAdvertDataListener.onAdFail(-200);
                }
                LogUtils.d("not request isLoadAd :" + adSdkParams.mVirtualModuleId);
                return false;
            }
        }
        if (VirtualIdParseHelper.isExternalAds(adSdkParams.mContext, adSdkParams.mVirtualModuleId)) {
            if (adSdkParams.mCdays < 3) {
                adSdkParams.mLoadAdvertDataListener.onAdFail(-400);
                LogUtils.d("not request isExternalAds adSdkParams.mCdays < 3 :" + adSdkParams.mVirtualModuleId);
                return false;
            }
        }

        if (isWaps(channel) || isXm(channel)) {
            return true;
        }
        if (adSdkParams.mCdays < 2) {
            adSdkParams.mLoadAdvertDataListener.onAdFail(-300);
            LogUtils.d("not request adSdkParams.mCdays < 2 :" + adSdkParams.mVirtualModuleId);
            return false;
        }
        return true;
    }
}
